package com.example.sortlistview01.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.nineoldandroids.view.ViewPropertyAnimator;

public class LetterSortSideBar extends LinearLayout {
	public static String[] letters = { "A", "B", "C", "D", "E", "F", "G", "H",
			"I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U",
			"V", "W", "X", "Y", "Z", "#" };
	private View[] views = new View[letters.length];
	private Context context;
	private int choose;

	public LetterSortSideBar(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		this.context = context;
		initView();
	}

	public LetterSortSideBar(Context context, AttributeSet attrs) {
		super(context, attrs);
		this.context = context;
		initView();
	}

	public LetterSortSideBar(Context context) {
		super(context);
		this.context = context;
		initView();
	}

	private void initView() {
		setOrientation(LinearLayout.VERTICAL);

		for (int i = 0; i < letters.length; i++) {
			views[i] = creatChildView(letters[i]);
			addView(views[i]);
		}

	}
    
	private TextView creatChildView(String text) {
		TextView child = new TextView(context);
		child.setTextSize(13);
		child.setText(text);
		child.setGravity(Gravity.CENTER);
		LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
				LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
		lp.gravity = Gravity.RIGHT;
		lp.weight = 1;
		child.setLayoutParams(lp);
		return child;
	}
	
	private void doAnim(View view , int distance){
		ViewPropertyAnimator.animate(view).translationX(distance).setDuration(0);
	}

	private void expandForActionDown(int index) {
		for (int i = 0; i < letters.length; i++) {
			TextView child = (TextView) views[i];
			if (index == i) {
//				child.setTranslationX(-150);
				doAnim(child, -150);
			} else if (index - 1 == i) {
//				child.setTranslationX(-100);
				doAnim(child, -100);
			} else if (index - 2 == i) {
//				child.setTranslationX(-50);
				doAnim(child, -50);
			} else if (index + 1 == i) {
//				child.setTranslationX(-100);
				doAnim(child, -100);
			} else if (index + 2 == i) {
//				child.setTranslationX(-50);
				doAnim(child, -50);
			}
		}
	}
	private void expandForActionMove(int index){
		for (int i = 0; i < letters.length; i++) {
			View child = views[i];
			if (index == i) {
//				child.setTranslationX(-150);
				doAnim(child, -150);
			} else if (index - 1 == i) {
//				child.setTranslationX(-100);
				doAnim(child, -100);
			} else if (index - 2 == i) {
//				child.setTranslationX(-50);
				doAnim(child, -50);
			} else if (index + 1 == i) {
//				child.setTranslationX(-100);
				doAnim(child, -100);
			} else if (index + 2 == i) {
//				child.setTranslationX(-50);
				doAnim(child, -50);
			}else{
//				child.setTranslationX(0);
				doAnim(child, 0);
			}
		}
	}
	 private void expandForActionUp(int index){
	    	for(int i = 0;i < letters.length;i++){
	    		View child = views[i];
//	    		child.setTranslationX(0);
	    		doAnim(child, 0);
	    	}
	    }
	@Override
	public boolean dispatchTouchEvent(MotionEvent event) {
		final int action = event.getAction();
		final float y = event.getY();
		final int oldChoose = choose;
		final int index = (int) (y / getHeight() * letters.length);
		switch (action) {
		case MotionEvent.ACTION_UP:
			expandForActionUp(index);
			break;
		case MotionEvent.ACTION_DOWN:
			expandForActionDown(index);
			break;
		case MotionEvent.ACTION_MOVE:
			expandForActionMove(index);
			break;
		default:
			break;
		}
		return true;

	}
}
